package com.liaowei.post.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liaowei.common.Constants;
import com.liaowei.common.ErrorCode;
import com.liaowei.exception.CustomerException;
import com.liaowei.model.domain.PostUser;
import com.liaowei.model.param.PostCommentStatusParam;
import com.liaowei.model.param.PostHistoryParam;
import com.liaowei.model.param.PostUserParam;
import com.liaowei.post.mapper.PostUserMapper;
import com.liaowei.post.openfeign.UserClient;
import com.liaowei.post.service.PostUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* @author LindaMan
* @description 针对表【wy_post_user(帖子-用户表)】的数据库操作Service实现
* @createDate 2023-05-11 08:31:59
*/
@Service
@RequiredArgsConstructor
public class PostUserServiceImpl extends ServiceImpl<PostUserMapper, PostUser>
    implements PostUserService{

    private final PostUserMapper postUserMapper;
    private final UserClient userClient;

    // 校验用户是否认证
    public void matchUser(Long userId,Long loginId){
        if (!loginId.equals(userId)){
            throw new CustomerException(Constants.PARAM_ERROR,"请先认证");
        }
    }


    /**
     * 根据id修改评论状态
     * @param commentStatusParam 评论参数
     * @return 提示信息
     */
    @Transactional
    @Override
    public String updateCommentStatus(PostCommentStatusParam commentStatusParam) {
        // 查询用户是否已经评论过
        int commentStatus = postUserMapper.selectCommentStatus(commentStatusParam);
        if (commentStatus == 1){
            throw new CustomerException(Constants.PARAM_ERROR,"已经评论过");
        }

        try {
            postUserMapper.updateCommentStatus(commentStatusParam);
        } catch (Exception e) {
            throw new CustomerException(ErrorCode.SYSTEM_ERROR);
        }

        return "修改成功";
    }

    /**
     * 保存postUser
     * @param postUserParam
     * @return 提示信息
     */
    @Transactional
    @Override
    public String savePostUser(PostUserParam postUserParam) {
        // 获取帖子id
        Long postId = postUserParam.getPostId();
        // 获取当前用户id
        Long userId = postUserParam.getUserId();
        // 获取当前认证的id
        Long loginId =  Long.parseLong(StpUtil.getLoginId().toString());

        // 判断当前要删除的是不是自己的评论
        matchUser(userId,loginId);

        // 构造查询构造器
        LambdaQueryWrapper<PostUser> wrapper = Wrappers.lambdaQuery(PostUser.class)
                .eq(PostUser::getPostId, postId)
                .eq(PostUser::getUserId, userId);
        PostUser postUser = postUserMapper.selectOne(wrapper);

        // 获取参数
        Integer collectStatus = postUserParam.getCollectStatus();
        Integer commentStatus = postUserParam.getCommentStatus();
        Integer starStatus = postUserParam.getStarStatus();
        try {
            if (BeanUtil.isEmpty(postUser)){
                postUser = new PostUser();
                // 新增
                postUser.setPostId(postId);
                postUser.setUserId(userId);
                postUser.setAccessNumber(1);
                postUser.setCommentStatus(commentStatus);
                postUser.setStarStatus(starStatus);
                postUser.setCollectStatus(collectStatus);
                postUserMapper.insert(postUser);
            }else {
                // 修改
                postUser.setAccessNumber(postUser.getAccessNumber() + 1); // 访问量 +1
                postUser.setCollectStatus(collectStatus);
                postUser.setCommentStatus(commentStatus);
                postUser.setUpdateTime(new Date());
                postUserMapper.updateById(postUser);
            }
            // 新增浏览历史记录
            PostHistoryParam historyParam = new PostHistoryParam();
            historyParam.setUserId(userId);
            historyParam.setPostId(postId);
            userClient.addPostHistory(historyParam);

        } catch (Exception e) {
            throw new CustomerException(ErrorCode.SYSTEM_ERROR);
        }

        return "保存成功";
    }
}




